package itbour.onetouchshow.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

import itbour.onetouchshow.R;
import itbour.onetouchshow.bean.HomeDetailslBean;
import itbour.onetouchshow.utils.UIUtils;

/**
 * Created by zjl_d on 2017/12/1.
 */

public class InchingFontListAdapter extends RecyclerView.Adapter {
    FontStyleListener onFontStyleListener;
    private Context context;
    private List<HomeDetailslBean.FontListBean> fontList;
    private String selectFn;

    public InchingFontListAdapter(Context context, List<HomeDetailslBean.FontListBean> fontList) {
        this.context = context;
        this.fontList = fontList;
    }

    public void setSelectFn(String selectFn) {
        this.selectFn = selectFn;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ItemViewHolder(View.inflate(parent.getContext(), R.layout.adapter_inching_font_list, null));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ImageView ivInchingFont = ((ItemViewHolder) holder).ivInchingFont;
        ImageView ivInchingMask = ((ItemViewHolder) holder).ivInchingMask;
        HomeDetailslBean.FontListBean fontsBean = fontList.get(position);

        String currentThumb;
        String thumb = fontsBean.getThumb();
        String thumbg = fontsBean.getHThumb();
        final String font = fontsBean.getFont();


        if (selectFn != null && font.equals(selectFn)) {
            fontsBean.getFont().equals(selectFn);
            ivInchingMask.setVisibility(View.VISIBLE);
            currentThumb = thumbg;
        } else {
            ivInchingMask.setVisibility(View.GONE);
            currentThumb = thumb;
        }
        Glide.with(context)
                .load(currentThumb)
                .apply(new RequestOptions()
                        .override(UIUtils.WHD()[0], UIUtils.dp2px(55))).into(ivInchingFont);

        ivInchingFont.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //当点击图片时 需要字体的名字

                if (onFontStyleListener != null) {
                    onFontStyleListener.onItemClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return fontList == null ? 0 : fontList.size();
    }

    public void setFontStyleListener(FontStyleListener onItemPlayClick) {
        this.onFontStyleListener = onItemPlayClick;
    }

    public interface FontStyleListener {
        // true add; false cancel
        void onItemClick(int position);
    }

    static class ItemViewHolder extends RecyclerView.ViewHolder {
        public ImageView ivInchingFont;
        public ImageView ivInchingMask;

        public ItemViewHolder(View itemView) {
            super(itemView);
            ivInchingFont = itemView.findViewById(R.id.iv_inching_font);
            ivInchingMask = itemView.findViewById(R.id.iv_inching_mask);
        }
    }
}

